/**
 * 2018年11月27日上午9:30:54
 * @author 谢进财
 */
package com.baihang.project.ds.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.framework.service.BaseService;
import com.baihang.project.ds.dao.SystemConfigDao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import redis.clients.jedis.Jedis;

@Service
public class SystemConfigService extends BaseService {
	@Autowired
	private SystemConfigDao dao;
	private  Jedis jedis = RedisKit.getJedis();
	public String getConfig() {
		List<Record> records = dao.queryForRecordList("select * from sys_config");
		String json= JsonKit.listToJson(records, 1);
		return json;
	}
	
	public void changeState(String state) {
		dao.update(String.format("update sys_config set Value='%s' where Name='State'", state));
	}
	public void changeRedisState(String state) {
		jedis.hset("systemConfig", "State", state);
	}
	
	public void save(String json) {
		JSONArray jsonArray = JSONArray.fromObject(json); 
		dao.batchUpdateSystemConfigList(jsonArray);
	}
	public void redisSave(String json) {
		JSONArray jsonArray = JSONArray.fromObject(json); 
		Map<String,String> configMap = new HashMap<String,String>();
		for(int i=0;i<jsonArray.size();i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);			
			configMap.put(jsonObject.getString("Name"), jsonObject.getString("Value"));
		}
		jedis.hmset("systemConfig", configMap);
	}
	@Transactional
	public void ClearRoom() {
		dao.update(String.format("update room set D_state=%d,D_Operation=null,D_SaleDate=null,D_SerialNumber=null ", 0));
		dao.update("update room set D_Monitor=0 where D_Monitor=2 or D_Monitor=3");//内控的房源恢复到0的状态
	}
	@Transactional
	public void clearHouse() {
		//清空楼栋
		dao.update("truncate table Bans");
		//清空房源
		dao.update("truncate table room");
		//清空用户
		dao.update("truncate table t_user");
		//清空顾问
		dao.update("truncate table saller");
		//清空认筹
		dao.update("truncate table t_renchou");
		//清空收藏
		dao.update("truncate table t_collection");
		//短信日志删除
		dao.update("truncate table smslog");
		//清空redis 保留配置
		jedis.del("jiage");
		jedis.del("rengou");
		jedis.del("suoding");
		jedis.del("zaixian");
	}
	
	
}
